<?php
class ruleModel extends model {
	public $rule;public $content;public function export($zym_16) {
		$zym_20 = $this->where(['id' => $zym_16])->find();
		$zym_15 = (new NovelSearch_SiteModel())->where(['id' => $zym_20['siteid']])->field('name,key,url')->find();
		$zym_20['sitename'] = $zym_15['name'];
		$zym_20['sitekey'] = $zym_15['key'];
		$zym_20['siteurl'] = $zym_15['url'];unset($zym_20['create_user_id'], $zym_20['update_user_id'], $zym_20['create_time'], $zym_20['update_time'], $zym_20['addnew'], $zym_20['newreplace'], $zym_20['collectallchapter'], $zym_20['dirsort'], $zym_20['id'], $zym_20['siteid']);
		$zym_14 = "***************************************\nPTCMS 规则导出文件 <{$zym_20['name']}>\n***************************************\n\n";
		$zym_14 .= base64_encode(serialize($zym_20));
		$this->pt->response->download($zym_14, 'ptcms_' . $zym_20['name'] . '_' . date('YmdHi') . '.txt', 'rule');}public function importAction() {}public function add($zym_21) {
		$zym_21['create_user_id'] = $_SESSION['admin']['userid'];
		$zym_21['create_time'] = NOW_TIME;return $this->insert($zym_21);}public function edit($zym_21, $zym_19) {
		$zym_21['update_user_id'] = $_SESSION['admin']['userid'];
		$zym_21['update_time'] = NOW_TIME;return $this->where($zym_19)->update($zym_21);}public function del($zym_19) {$this->where($zym_19)->delete();}public function geteditinfo($zym_16) {$zym_20 = $this->where(array('id' => $zym_16))->find();foreach ($zym_20 as $zym_13 => &$zym_23) {if (strpos($zym_23, '[') !== false && $zym_23{0} == '{') {$zym_23 = json_decode($zym_23, true);}}return $zym_20;}public function getrule($zym_16) {$zym_20 = $this->where(array('id' => $zym_16))->find();if ($zym_20) {return $this->parseData2Rule($zym_20);} else {return array();}}public function getrulebysiteid($zym_16) {$zym_20 = $this->where(array('siteid' => $zym_16))->find();if ($zym_20) {return $this->parseData2Rule($zym_20);} else {return array();}}public function parseData2Rule($zym_20) {
		foreach ($zym_20 as $zym_13 => &$zym_23) {
			if (substr($zym_13, 0, 4) == 'url_') {
				$zym_23 = json_decode($zym_23, true);if (strpos($zym_23['rule'], '[内容]') || (strpos($zym_23['rule'], '(') && strpos($zym_23['rule'], ')'))) {
					$zym_23['rule'] = $this->parseMatchRule($zym_23['rule']);
					$zym_23['option'] = (!empty($zym_23['singleline'])) ? '' : 's';
					$zym_23['option'] .= (!empty($zym_23['ignorecase'])) ? 'i' : '';}} elseif (strpos($zym_13, '_')) {
				if (empty($zym_23) || $zym_23{0} !== '{') {
					continue;
				}

				$zym_23 = json_decode($zym_23, true);
				$zym_23['rule'] = $this->parseMatchRule(strval($zym_23['rule']));
				$zym_23['option'] = (!empty($zym_23['singleline'])) ? '' : 's';
				$zym_23['option'] .= (!empty($zym_23['ignorecase'])) ? 'i' : '';} elseif (strpos($zym_23, '[内容]') && $zym_23{0} == '{') {
				$zym_23 = json_decode($zym_23, true);
				$zym_23['rule'] = $this->parseMatchRule(strval($zym_23['rule']));
				$zym_23['option'] = (!empty($zym_23['singleline'])) ? '' : 's';
				$zym_23['option'] .= (!empty($zym_23['ignorecase'])) ? 'i' : '';} else {continue;}unset($zym_23['singleline'], $zym_23['ignorecase']);
			$zym_23['replace'] = $this->parseReplaceRule($zym_23['replace']);}$this->rule = $zym_20;return $zym_20;}protected function parseReplaceRule($zym_10) {
		$zym_22 = array();foreach ($zym_10 as $zym_6) {
			$zym_5 = array();
			$zym_5['method'] = empty($zym_6['type']) ? 0 : 1;
			$zym_5['option'] = empty($zym_6['singleline']) ? 's' : '';
			$zym_5['option'] .= empty($zym_6['ignorecase']) ? '' : 'i';
			$zym_5['rule'] = $zym_6['search'];if (isset($zym_6['replace'])) {$zym_5['rule'] .= '♂' . $zym_6['replace'];}$zym_22[] = $zym_5;}return $zym_22;}protected function parseMatchRule($zym_7) {$zym_12 = array('{' => '\{', '}' => '\}', '[内容]' => '(.*?)', '[数字]' => '\d*', '[空白]' => '\s*', '[任意]' => '.*?', '[参数]' => '[^\>\<]*?', '[属性]' => '[^\>\<\'"]*?');return strtr($zym_7, $zym_12);}protected function success($zym_5) {return array('status' => 1, 'data' => $zym_5);}protected function error($zym_11) {return array('status' => 0, 'msg' => $zym_11);}protected function mergeParam($zym_8) {$zym_5 = array();foreach ($zym_8 as $zym_17 => $zym_9) {foreach ($zym_9 as $zym_13 => $zym_23) {$zym_5[$zym_13][$zym_17] = $zym_23;}}return $zym_5;}public function getlist() {
		$zym_18 = $this->select();if (!$zym_18) {
			return array();
		}
		foreach ($zym_18 as &$zym_23) {
			$zym_23['sitename'] = dc::get('novelsearch_site', $zym_23['siteid'], 'name');
			$zym_23['create_username'] = dc::get('user', $zym_23['create_user_id'], 'name');
			$zym_23['update_username'] = dc::get('user', $zym_23['update_user_id'], 'name');
			$zym_23['create_time'] = $zym_23['create_time'] ? date('Y-m-d H:i', $zym_23['create_time']) : '';
			$zym_23['update_time'] = $zym_23['update_time'] ? date('Y-m-d H:i', $zym_23['update_time']) : '';
			$zym_23['url_edit'] = U('rule.collect.edit', array('id' => $zym_23['id']));
			$zym_23['url_test'] = U('rule.collect.test', array('id' => $zym_23['id']));
			$zym_23['url_run'] = U('rule.collect.run', array('id' => $zym_23['id']));}return $zym_18;}}
?>